// packageOther/pages/opinion/opinion.js
import {
    wxfileTobase64
} from '../../../utils/util.js'
import {
    suggestion
} from '../../../api/app.js'
import user from '../../../utils/user.js'
const app = getApp()
Page({

    /**
	 * 页面的初始数据
	 */
    data: {
        type: 1,
        content: '',
        phone: '',
        typeList: [{
            text: '投诉',
            key: 1
        },
        {
            text: '建议',
            key: 2
        },
        {
            text: '故障',
            key: 3
        },
        {
            text: '其他',
            key: 4
        },
        ],
        fileList: []
    },


    // 切换投诉类别
    checkType(event) {
        this.setData({
            type: event.currentTarget.dataset.key
        })
    },

    // 上传图片
    upload() {
        const t = this
        wx.chooseImage({
            count: 4,
            sizeType: ['compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
                console.log(res)
                // tempFilePath可以作为img标签的src属性显示图片
                const tempFilePaths = t.data.fileList
                if ((tempFilePaths.length + res.tempFilePaths.length) > 4) {
                    app.toast('最多只能上传4张图片！')
                    return 0
                }
                tempFilePaths.push(...res.tempFilePaths)
                t.setData({
                    fileList: tempFilePaths
                })
            }
        })
    },
    inputchange(event) {
        this.setData({
            [event.currentTarget.dataset.model]: event.detail.value
        })
    },
    submit() {
        if (!this.data.content) {
            app.toast('请填写投诉内容！')
            return 0
        }
        if (this.data.phone !== '' && !/^1\d{10}$/.test(this.data.phone)) {
            app.toast('请填写正确的手机号码！')
            return 0
        }
        const promiseList = []
        for (const i in this.data.fileList) {
            promiseList.push(wxfileTobase64(this.data.fileList[i]))
        }
        Promise.all(promiseList).then(result => {
            suggestion({
                type: this.data.type,
                userId: user.userId,
                suggestion: this.data.content,
                companyId: user.companyId,
                phone: this.data.phone,
                createUserId: user.companyId,
                params: {
                    photos: result
                }
            }).then(()=>{
                wx.navigateTo({
                    url: '/pages/success/success?title=反馈成功&content=感谢您的反馈，我们会继续加油&back=' + 'home'
                })
            })
        })

    },

})
